﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmGetNewDataList : DockContent
    {
        PositionDetailClient wcfPD;
        //BranchClient wcfBranch;
        //DepartmentClient wcfDepartment;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        int itemCount = 0;
        int pageSize = UIParamater.pageSize;
        int totalPage = 0;
        int currPage = 0;
        int employeeID = 0;
        int department_ID;
        int branch_ID;
        DateTime _date;
        string _month;
        string _year;
        bool _IsBHTN;
        bool _PhatSinhBH;
        bool _IsFactor;
        string _selectedID = ",";
        public string SelectedID
        {
            get { return _selectedID; }
            set { _selectedID = value; }
        }
        string[] _emplID = null;
        private string _FullName;
        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }
        string _Code;
        public string Code
        {
            get { return _Code; }
            set { _Code = value; }
        }
        private int _EmployeeID;
        public int EmployeeID
        {
            get { return _EmployeeID; }
            set { _EmployeeID = value; }
        }
        string _SIBookNo;
        public string SIBookNo
        {
            get { return _SIBookNo; }
            set { _SIBookNo = value; }
        }
        bool _ChangeDepartment = false;
        PositionDetailInfo[] _PDInfo;
        public frmGetNewDataList(RoleRightInfo _listRole)
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;
            wcfPD = Service.PositionDetail();
            txtKeyWord.Focus();
            listRole = _listRole;
            BuildVoucherGrid();
            UISetting.LoadTreeView3(ref tvBranch);
            //LoadData();
        }
        private void BuildVoucherGrid()
        {
            dgvEmployeeList.Columns.Clear();
            //Thêm cột Checkbox
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.Name = "Check";
            col.HeaderText = "Chọn";
            col.Width = 30;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvEmployeeList.Columns.Add(col);
            col.Visible = true;
            //col.ReadOnly = false;
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvEmployeeList, "RowNumber", UpdateUI.GetString("stt"), "C", 30, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Code", "Mã NV", "L", 80, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "FullName", UpdateUI.GetString("hovaten"), "L", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_DateOfBirth", "Ngày sinh", "C", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Sex", "GT", "C", 60, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Position_Name", "Chức vụ", "L", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_SIBookNo", "Số sổ BH", "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "PD_BeginDate", "Từ tháng", "", 80, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "PD_BasicSalary", "ML", "R", 100, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "PD_BasicFactor", "HSL", "R", 60, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "PD_PositionFactor", "HS Cvụ", "R", 80, true, true);
        }


        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void LoadData()
        {
            DataSet ds = wcfPD.PositionDetail_GetForInsurance(DateTime.Now.Date, branch_ID, department_ID, txtKeyWord.Text.Trim());
            int n = ds.Tables[0].Rows.Count;
            PageIndex();
            dgvEmployeeList.Rows.Clear();
            for (int i = 0; i < n; i++)
            {
                dgvEmployeeList.Rows.Insert(i, 1);
                dgvEmployeeList.Columns["FullName"].Frozen = true;
                dgvEmployeeList.Rows[i].Cells["Employee_ID"].Value = ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvEmployeeList.Rows[i].Cells["RowNumber"].Value = ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_Code"].Value = ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvEmployeeList.Rows[i].Cells["FullName"].Value = ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                dgvEmployeeList.Rows[i].Cells["Position_Name"].Value = ds.Tables[0].Rows[i]["Position_Name"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_DateOfBirth"].Value
                    = ds.Tables[0].Rows[i]["Employee_DateOfBirth"] == DBNull.Value ? ""
                    : Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Employee_Sex"].Value = ds.Tables[0].Rows[i]["Employee_Sex"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_SIBookNo"].Value = ds.Tables[0].Rows[i]["Employee_SIBookNo"].ToString();
                dgvEmployeeList.Rows[i].Cells["PD_BeginDate"].Value = ds.Tables[0].Rows[i]["PD_BeginDate"] != DBNull.Value ?  Convert.ToDateTime(ds.Tables[0].Rows[i]["PD_BeginDate"]).ToString("MM/yyyy"):"";
                
                dgvEmployeeList.Rows[i].Cells["PD_BasicSalary"].Value = Globals.DB_GetValue<decimal>(ds.Tables[0].Rows[i]["PD_BasicSalary"],0).ToString("#0.0##");
                dgvEmployeeList.Rows[i].Cells["PD_BasicFactor"].Value = Globals.DB_GetValue<decimal>(ds.Tables[0].Rows[i]["PD_BasicFactor"],0).ToString("#0.0##");
                dgvEmployeeList.Rows[i].Cells["PD_PositionFactor"].Value = Globals.DB_GetValue<decimal>(ds.Tables[0].Rows[i]["PD_PositionFactor"], 0).ToString("#0.0##");
                if (_selectedID.Contains("," + ds.Tables[0].Rows[i]["Employee_ID"].ToString() + ","))
                    dgvEmployeeList.Rows[i].Cells["Check"].Value = true;
                else
                    dgvEmployeeList.Rows[i].Cells["Check"].Value = false;
            }
            dgvEmployeeList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void GetSelectedID()
        {
            dgvEmployeeList.EndEdit();
            foreach (DataGridViewRow row in dgvEmployeeList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                {
                    if (!_selectedID.Contains("," + row.Cells["Employee_ID"].Value.ToString() + ","))
                        _selectedID += row.Cells["Employee_ID"].Value.ToString() + ",";
                }
                else
                {
                    string sOld = "," + row.Cells["Employee_ID"].Value.ToString() + ",";
                    if (_selectedID.Contains(sOld))
                        _selectedID = _selectedID.Replace(sOld, ",");
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvEmployeeList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dlgResult = DialogResult.OK;
            this.Close();
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtKeyWord.Text = "";
            if (e.Node.Level == 0)
            {
                branch_ID = Program.intBranchID;
                department_ID = 0;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level == 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();
            }
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            currPage = 0;
            LoadData();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (dgvEmployeeList.SelectedRows.Count > 0)
                dgvEmployeeList_CellDoubleClick(null, null);

        }

        private void frmChooseEmployeeList_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void ChkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkCheckAll.Checked)
            {
                for (int i = 0; i < dgvEmployeeList.Rows.Count; i++)
                {
                    dgvEmployeeList.Rows[i].Cells["Check"].Value = true;
                }
            }
            else
            {
                for (int i = 0; i < dgvEmployeeList.Rows.Count; i++)
                {
                    dgvEmployeeList.Rows[i].Cells["Check"].Value = false;
                }
            }
            GetSelectedID();
        }
    }
}

